package com.blog.controller;


import com.blog.common.result.R;
import com.blog.common.result.RPage;
import com.blog.entity.po.Category;
import com.blog.service.ICategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

/**
 * <p>
 * 分类表 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-03-24
 */
@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {

    @Autowired
    private ICategoryService categoryService;


    /**
     * 获取分类列表
     * @param currentPage 当前页码
     */
    @GetMapping
    public R<RPage<Category>> getList(Integer currentPage ,Integer pageSize ){
        log.info("获取分类列表,当前页参数为：{}", currentPage );
       RPage<Category> voPage = categoryService.getListByPage(currentPage , pageSize);
        return R.success(voPage);
    }

    /**
     * 删除分类
     * @param id 分类id
     * @return
     */
    @DeleteMapping("{id}")
    public R delById(@PathVariable String id){
        log.info("删除分类,id为：{}", id);
        categoryService.delById(id);
        return R.success();
    }


    /**
     * 新增分类
     * @param category 分类名 、分类描述
     * @return
     */
    @PostMapping
    public R saveCategory(@RequestBody Category category){
        log.info("新增分类,参数为：{}", category);
        categoryService.saveCategory(category);
        return R.success();
    }


    @PutMapping
    public R editCategory(@RequestBody Category category){
        log.info("修改分类,参数为：{}", category);
        categoryService.editCategory(category);
        return R.success();
    }

}
